package org.springblade.modules.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.modules.classes.entity.TrainingPlanEntity;
import org.springblade.modules.library.entity.AllowUserEntity;
import org.springblade.modules.library.service.IAllowUserService;
import org.springblade.modules.material.entity.CoursewareEntity;
import org.springblade.modules.material.service.ICoursewareService;
import org.springblade.modules.material.vo.CoursewareVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 学习资料相关功能接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-client/training")
@Api(value = "客户端资源相关功能接口" ,tags ="客户端资源相关功能接口" )
public class ClientTrainingMaterialController {

	private final ICoursewareService coursewareService;
	private final IAllowUserService allowUserService;



	/**
	 * 资料分享分页
	 */
	@GetMapping("/material-list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "资料分享分页列表", notes = "")
	public R trainingMaterialList(CoursewareVO entity, Query query) {
		LambdaQueryWrapper<AllowUserEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AllowUserEntity::getAllowUserId, AuthUtil.getUserId());
		List<AllowUserEntity> allowerlist = allowUserService.list(lambdaQueryWrapper);

//		IPage<AllowUserEntity> pages = allowUserService.page(Condition.getPage(query), lambdaQueryWrapper);
		if(CollectionUtil.isEmpty(allowerlist))		return R.success("暂无数据");

		LambdaQueryWrapper<CoursewareEntity> coursewarQueryWrapper=new LambdaQueryWrapper<>();
		coursewarQueryWrapper.in(CoursewareEntity::getId,allowerlist.stream().map(AllowUserEntity::getDataShareId).collect(Collectors.toList()));
		if(ObjectUtil.isNotEmpty(entity.getSearchStartTime())) coursewarQueryWrapper.gt(CoursewareEntity::getCreateTime,entity.getSearchStartTime());
		if(ObjectUtil.isNotEmpty(entity.getSearchEndTime())) coursewarQueryWrapper.lt(CoursewareEntity::getCreateTime,entity.getSearchEndTime());
		if(ObjectUtil.isNotEmpty(entity.getCoursewareName())) coursewarQueryWrapper.like(CoursewareEntity::getCoursewareName,entity.getCoursewareName());
		return R.data(coursewareService.page(Condition.getPage(query),coursewarQueryWrapper));
	}


}
